/*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Leandro Melo de Sales (leandro@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.service.conference;

import java.util.Map;

import javax.media.MediaLocator;

import br.ufal.tci.nexos.arcolive.ArCoLIVEServiceInstance;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotAddParticipantException;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotCreateTransmitterException;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotStartServiceException;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVEException;
import br.ufal.tci.nexos.arcolive.logger.ArCoLIVELogRepository;
import br.ufal.tci.nexos.arcolive.participant.ArCoLIVEParticipant;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */
public class ConferenceOneToMany extends ArCoLIVEServiceInstance {

	private Conference0neToManyImpl conferenceImpl;

	public ConferenceOneToMany(int serviceId, int serviceInstanceId, String name, String description, int maxParticipant, Map additionalParameters) {
		super(serviceId, serviceInstanceId, name, description, maxParticipant, additionalParameters);
	}
	
	/**
     * Starts the transmission. Returns null if transmission started ok.
     * Otherwise it returns a string with the reason why the setup failed.
     */
    public synchronized void startService() throws ArCoLIVECannotStartServiceException {
    	String mediaLocatorURL = this.getAttribute("mediaLocatorURL");
    	MediaLocator mediaLocator = new MediaLocator(mediaLocatorURL);
    	int startPort = this.getIntAttribute("startPort");
    	this.conferenceImpl = new Conference0neToManyImpl(mediaLocator, startPort);
		try {
			// Create a processor for the specified media locator
			// and program it to output JPEG/RTP
			this.conferenceImpl.createProcessor();
		} catch (Exception e) {
			ArCoLIVELogRepository.getInstance().log(e.getMessage());
		}
		
		// Create an RTP session to transmit the output of the
		// processor to the specified IP address and port no.
			try {
				this.conferenceImpl.createTransmitter();
			} catch (ArCoLIVECannotCreateTransmitterException e) {
				throw new ArCoLIVECannotStartServiceException("Error in attempt to start service", e);
			}
		
		
		// Start the transmission
		this.conferenceImpl.getProcessor().start();
		this.setServiceStatus(1);
		
    }
	
	public void addParticipant(ArCoLIVEParticipant participant) throws ArCoLIVECannotAddParticipantException {
		try {
			this.participantGroup.addParticipant(participant);
			
			this.conferenceImpl.addTarget(participant, this.participantGroup);
			ArCoLIVELogRepository.getInstance().log("Stream created for participant " + participant + " in conference " + this.getServiceInstanceDescriptor().getName());
			
		} catch (ArCoLIVEException ale) {
			throw new ArCoLIVECannotAddParticipantException(ale);
		}
	}
	
	public void removeParticipant(ArCoLIVEParticipant alParticipant) {
		//TEM Q IMPLEMENTAR ESSE M?TODO:
		// remover o participant do grupo
		// se removido disconecta o participant
	}
	
	public int getPort() {
		return this.conferenceImpl.getPort();
	}

    /**
     * Stops the transmission if already started
     */
    public void stopService() {
    	this.conferenceImpl.stop();
		this.getServiceStatus().setStatusId(0);
    }	

}
